package com.kamistoat.meimeimall.meimeimallpayment.web;

import com.kamistoat.meimeimall.common.To.paymentTo.ReconciliationTaskTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.constant.DatabaseConstant;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallpayment.dao.BillRecordDao;
import com.kamistoat.meimeimall.meimeimallpayment.entity.BillRecordEntity;
import com.kamistoat.meimeimall.meimeimallpayment.entity.ReconciliationErrorEntity;
import com.kamistoat.meimeimall.meimeimallpayment.entity.ReconciliationTaskEntity;
import com.kamistoat.meimeimall.meimeimallpayment.service.BillRecordService;
import com.kamistoat.meimeimall.meimeimallpayment.service.PaymentBillJoinService;
import com.kamistoat.meimeimall.meimeimallpayment.service.ReconciliationErrorService;
import com.kamistoat.meimeimall.meimeimallpayment.service.ReconciliationTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "对账任务页面")
@RestController
@RequestMapping("/web")
public class ReconciliationPageController {
    protected Logger logger = LoggerFactory.getLogger(ReconciliationPageController.class);

    @Autowired
    ReconciliationTaskService reconciliationTaskService;
    @Autowired
    ReconciliationErrorService reconciliationErrorService;

    /**
     * 制作测试数据，包括本地交易流水和第三方订单数据
     *
     * @param size 测试数据量
     * @param day  测试数据天数
     * @return R对象
     */
    @ApiOperation("制作测试数据。包括本地交易流水和第三方订单数据")
    @GetMapping("/construct_test_data")
    public R constructTestData(@RequestParam("size") Integer size, @RequestParam("day") String day) {
        reconciliationTaskService.constructTestData(size, day);
        return R.ok();
    }


    /**
     * 获取所有对账任务，最晚创建的在前
     */
    @ApiOperation("获取所有对账任务，按照创建时间排序")
    @GetMapping("/get_all_reconciliation_task")
    //@LoginRequired
    @DubboAndHTTPExceptionCatch
    public R getAllReconciliationTask(@RequestParam(value = "pageSize", defaultValue = DatabaseConstant.DEFAULT_PAGE_SIZE) Integer pageSize,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page) {
        List<ReconciliationTaskEntity> reconciliationTaskEntityList = reconciliationTaskService.getAllReconciliationTask(pageSize, page);
        return R.ok().put("reconciliationTaskEntityList", reconciliationTaskEntityList);
    }

    /**
     * 新建对账任务。仅仅允许提交对账起始时间和终止时间
     *
     * @param reconciliationTaskTo To类
     * @return R对象
     */
    @ApiOperation("新建对账任务")
    @PostMapping("/new_reconciliation_task")
    //@LoginRequired
    @DubboAndHTTPExceptionCatch
    public R newReconciliationTask(@RequestBody ReconciliationTaskTo reconciliationTaskTo) {
        reconciliationTaskService.newReconciliationTask(reconciliationTaskTo);
        return R.ok();
    }

    /**
     * 开始一项对账任务。将向消息队列提交对账开始消息
     */
    @ApiOperation("开始一项对账任务，将向消息队列提交对账开始消息，由消息队列后台执行")
    @PostMapping("/start_reconciliation_task/{taskId}")
    //@LoginRequired
    @DubboAndHTTPExceptionCatch
    public R startReconciliationTask(@PathVariable("taskId") Long taskId) {
        reconciliationTaskService.startReconciliationTask(taskId);
        return R.ok("对账任务开始成功，前端开始监听对账结果");
    }

    @ApiOperation("获取一项对账任务的对账结果，即该任务的所有坏账")
    @GetMapping("/error_list_of_reconciliation_task/{taskId}")
    //@LoginRequired
    @DubboAndHTTPExceptionCatch
    public R errorListOfReconciliationTask(@PathVariable("taskId") Long taskId,
                                           @RequestParam(value = "pageSize", defaultValue = DatabaseConstant.DEFAULT_PAGE_SIZE) Integer pageSize,
                                           @RequestParam(value = "page", defaultValue = "1") Integer page) {
        List<ReconciliationErrorEntity> errorList = reconciliationErrorService.getErrorListByTaskId(taskId, pageSize, page);
        return R.ok().put("errorList", errorList);
    }

}
